/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import database.InventoryDAO;
import database.ProductDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Product;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;


/**
 *
 * @author Trang
 */
@Namespace("/Administration/Inventory")
public class InventoryAction extends ActionSupport{
    public InventoryDAO inventoryDAO = new InventoryDAO();
    private List<Product> products = new ArrayList<Product>();

    private Map<Integer,String> sorts;

    private String keyword;
    public  InventoryAction()
    {
        sorts = new HashMap<Integer, String>();
        sorts.put(1, "Product Code");
        sorts.put(2, "Product Name");
        sorts.put(3, "Unit Price");
        sorts.put(4, "Stock Quantity");
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
    
    
    public Map<Integer, String> getSorts() {
        return sorts;
    }

    public void setSorts(Map<Integer, String> sorts) {
        this.sorts = sorts;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }
    private Integer sort;
    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
    @Action(value = "inventory", results = {
        @Result(name = "success", location = "/manageReport/w_inventory.jsp")
    })
    public String OrderManagement(){
         Map session = ActionContext.getContext().getSession();
                session.put("product", 1);
        products = inventoryDAO.getProductForInventory(1);
        int[] idProduct = new int[products.size()];
        int i = 0;
        for(Product item: products)
        {
            idProduct[i] = item.getIdProduct();
            i++;
        }
        session.put("idProduct", idProduct);  
        return SUCCESS;
    }
    @Action(value = "sort-inventory", results = {
        @Result(name = "success", location = "/manageReport/w_inventory.jsp")
    })
    public String Sort(){
         Map session = ActionContext.getContext().getSession();
                session.put("product", 2);
        if(sort==1)
        products = inventoryDAO.getProductForInventory(1);
        else if(sort == 2)
            products = inventoryDAO.getProductForInventory(2);
        else if(sort == 3)
            products = inventoryDAO.getProductForInventory(3);
        else
            products = inventoryDAO.getProductForInventory(4);
        int[] idProduct = new int[products.size()];
        int i = 0;
        for(Product item: products)
        {
            idProduct[i] = item.getIdProduct();
            i++;
        }
        session.put("idProduct", idProduct);
        return SUCCESS;
    }
    @Action(value = "search", results = {
        @Result(name = "success", location = "/manageReport/w_inventory.jsp"),
        @Result(name = "error", location = "/manageReport/w_inventory.jsp")
    })
    public String searchProduct() {
         Map session = ActionContext.getContext().getSession();
                
        if (keyword == null || keyword.length() < 3) {
            addActionError("Keyword must be greater than 2.");
            products = new InventoryDAO().getProductForInventory(1);
            session.put("product", 1);
            int[] idProduct = new int[products.size()];
            int i = 0;
            for(Product item: products)
            {
                idProduct[i] = item.getIdProduct();
                i++;
            }
            session.put("idProduct", idProduct);
            return ERROR;
        } else {
            products = new ProductDAO().searchForProductsByKeyword(keyword);
            session.put("product", 3);
            int[] idProduct = new int[products.size()];
            int i = 0;
            for(Product item: products)
            {
                idProduct[i] = item.getIdProduct();
                i++;
            }
            session.put("idProduct", idProduct);
            return SUCCESS;
        }
    }
}
